package com.issoft.studentresitor.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.issoft.studentresitor.domain.dto.ClazzDTO;
import com.issoft.studentresitor.domain.pojo.Clazz;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.pojo.Student;
import com.issoft.studentresitor.domain.query.ClazzQuery;
import com.issoft.studentresitor.domain.vo.ClazzVo;
import com.issoft.studentresitor.domain.vo.QueryClazzVo;
import com.issoft.studentresitor.domain.vo.StudentVo;
import com.issoft.studentresitor.mapper.StudentMapper;
import com.issoft.studentresitor.service.IClazzService;
import com.issoft.studentresitor.service.IStudentService;
import com.issoft.studentresitor.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GJH
 * @since 2025-09-14
 */
@Api(tags = "班级管理")
@RestController
@Slf4j
@RequestMapping("/admin/clazz")
public class ClazzController {
    @Autowired
    private IClazzService clazzService;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private IStudentService studentService;

    /**
     * 分页查询
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result queryClazzPage( @RequestParam(required = false) String className,
                                  @RequestParam(required = false) Integer majorId,
                                  @RequestParam(required = false) String grade,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pageSize){
        ClazzQuery clazzQuery = new ClazzQuery();
        clazzQuery.setClassName(className);
        clazzQuery.setMajorId(majorId);
        clazzQuery.setGrade(grade);
        clazzQuery.setPage(page);
        clazzQuery.setPageSize(pageSize);
        log.info("分页查询班级信息,参数:{}",clazzQuery);
        PageResult<QueryClazzVo> result = clazzService.queryClazzPage(clazzQuery);
        return Result.success(result);
    }
    /**
     * 根据id删除班级
     */
    @ApiOperation("根据id删除班级")
    @DeleteMapping("/{id}")
    @Transactional
    public Result delete(@PathVariable Integer id){
        boolean result = clazzService.removeById(id);
        log.info("删除班级信息,参数:{}",id);
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("class_id", null)  // 将class_id设置为null
                .eq("class_id", id);     // 条件为class_id等于给定的id

        // 执行更新操作
        int delete = studentMapper.update(null, updateWrapper);
        boolean studentsUpdated = delete >= 0;
        return (result && studentsUpdated) ? Result.success("班级已经解散") : Result.error("删除失败");
    }
    /**
     * 批量删除班级
     */
    @ApiOperation("批量删除班级")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Integer> ids){
        boolean result = clazzService.removeByIds(ids);
        return result ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
    /**
     * 根据id查询班级信息
     */
    @ApiOperation("根据班级id查询班级信息")
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id){
        Clazz clazz = clazzService.getById(id);
        ClazzVo clazzVo = new ClazzVo();
        BeanUtil.copyProperties(clazz,clazzVo);
        return clazz != null ? Result.success(clazzVo) : Result.error("班级不存在");
    }
    /**
     * 新增班级
     */
    @ApiOperation("新增班级")
    @PostMapping
    public Result add(@RequestBody ClazzDTO clazzDTO){
        Clazz clazz = new Clazz();
        BeanUtil.copyProperties(clazzDTO,clazz);
        boolean result = clazzService.save(clazz);
        return result ? Result.success() : Result.error("添加失败");
    }
    /**
     * 修改班级信息
     */
    @ApiOperation("修改班级信息")
    @PutMapping
    public Result update(@RequestBody ClazzDTO clazzDTO){
        Clazz clazz = new Clazz();
        BeanUtil.copyProperties(clazzDTO,clazz);
        boolean result = clazzService.updateById(clazz);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }
    /**
     * 根据id查询班级内的所有学生
     */
    @ApiOperation("根据id查询班级内的所有学生")
    @GetMapping("/student/{id}")
    public Result getStudent(@PathVariable Integer id){
        List<StudentVo> students = clazzService.getStudent(id);
        return students != null ? Result.success(students) : Result.error("班级内没有学生");
    }
    /**
     * 根据学生id将学生表的classid设置为空
     */
    @ApiOperation("根据学生id将学生表的classid设置为空")
    @PutMapping("/student/{id}")
    public Result updateStudent(@PathVariable Integer id){
        return studentMapper.updateStudent(id) > 0 ? Result.success("将学生移除成功") : Result.error("修改失败");
    }
    /**
     * 将没有班级的学生拉入本班级
     * classId 班级表的class_id
     * id 学生表的id
     */
    @ApiOperation("将没有班级的学生拉入本班级")
    @PostMapping("/pullstudent")
    public Result pullStudent(@RequestParam Integer classId,
                              @RequestParam Integer id){
        int i=studentMapper.pullStudent(classId,id);
        return i>0?Result.success("拉人成功"):Result.error("拉入失败");
    }
    @ApiOperation("批量拉没有班级的学生加入本班级")
    @PostMapping("/pullstudentbatch")
    public Result pullStudentBatch(@RequestParam Integer classId,
                                   @RequestParam List<Integer> ids){
        int i=studentMapper.pullStudentBatch(classId,ids);
        return i>0?Result.success("拉人成功"):Result.error("拉入失败");
    }
    //统计班级班级总数
    @ApiOperation("统计班级班级总数")
    @GetMapping("/count")
    public Result count(){
        Long count = clazzService.count();
        return Result.success(count);
    }
    //统计一共有多少学生
    @ApiOperation("统计一共有多少学生")
    @GetMapping("/countStudent")
    public Result countStudent(){
        Long count = studentService.count();
        return Result.success(count);
    }
    //统计空班级数量
    @ApiOperation("统计空班级数量")
    @GetMapping("/countEmptyClass")
    public Result countEmptyClass(){
        Long count = clazzService.countEmptyClass();
        return Result.success(count);
    }
    //查询出班级列表
    @ApiOperation("查询出班级列表")
    @GetMapping("/list")
    public Result list(){
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }
}
